package com.youpai.platform.biggod.service.service.impl;

import com.youpai.platform.biggod.service.convert.BigGodConverter;
import com.youpai.platform.common.enums.ProcessCodeEnum;
import com.youpai.platform.common.exceptions.BaseBizException;
import com.youpai.platform.common.util.StringUtils;
import com.youpai.platform.common.util.id.IdGenerator;
import com.youpai.platform.biggod.service.entity.po.CategoryPO;
import com.youpai.platform.biggod.service.mapper.CategoryMapper;
import com.youpai.platform.biggod.service.service.CategoryService;
import com.youpai.platform.biggod.web.entity.dto.CategoryDto;
import com.youpai.platform.biggod.web.entity.req.CategoryReq;
import com.youpai.platform.biggod.web.entity.resp.CategoryResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @time: 2022/11/12
 * @author: huanghao
 * @description: 品类service实现类
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public CategoryResp createCategory(CategoryReq req) {
        if(StringUtils.isEmpty(req.getCategoryName())){
            log.error("品类名称不能为空：categoryName - {}", req.getCategoryName());
            throw new BaseBizException(ProcessCodeEnum.CATEGORY_NAME_ISNULL);
        }
        if(StringUtils.isEmpty(req.getCategoryDesc())){
            log.error("品类描述不能为空：categoryDesc - {}", req.getCategoryDesc());
            throw new BaseBizException(ProcessCodeEnum.CATEGORY_DESC_ISNULL);
        }
        if(StringUtils.isEmpty(req.getCategoryUnit())){
            log.error("品类单位不能为空：categoryUnit - {}", req.getCategoryUnit());
            throw new BaseBizException(ProcessCodeEnum.CATEGORY_UNIT_ISNULL);
        }
        if(StringUtils.isEmpty(req.getCategoryImg())){
            log.error("品类图标不能为空：categoryImg - {}", req.getCategoryName());
            throw new BaseBizException(ProcessCodeEnum.CATEGORY_IMG_ISNULL);
        }
        Long categoryId = IdGenerator.generateId();
        CategoryPO categoryPO = new CategoryPO(
                categoryId, req.getCategoryName(), req.getCategoryDesc(),
                req.getCategoryUnit(), req.getCategoryImg());
        boolean flag = categoryMapper.add(categoryPO) > 0;
        if(flag){
            return new CategoryResp().setHintText("新增品类成功!");
        }else {
            log.error("新增品类失败");
            throw new BaseBizException(ProcessCodeEnum.CATEGORY_ADD_EXCEPTION);
        }
    }

    @Override
    public CategoryResp updateCategory(CategoryReq req) {
        CategoryPO categoryPO = categoryMapper.get(Long.valueOf(req.getCategoryId()));
        if(categoryPO == null){
            log.error("当前品类不存在");
            throw new BaseBizException(ProcessCodeEnum.CATEGORY_NOT_REPETITION);
        }
        if(StringUtils.isNotEmpty(req.getCategoryName())) {
            categoryPO.setCategoryName(req.getCategoryName());
        }
        if(StringUtils.isNotEmpty(req.getCategoryDesc())) {
            categoryPO.setCategoryDesc(req.getCategoryDesc());
        }
        if(StringUtils.isNotEmpty(req.getCategoryUnit())) {
            categoryPO.setCategoryUnit(req.getCategoryUnit());
        }
        if(StringUtils.isNotEmpty(req.getCategoryImg())) {
            categoryPO.setCategoryImg(req.getCategoryImg());
        }
        boolean flag = categoryMapper.update(categoryPO) > 0;
        if(flag){
            return new CategoryResp().setHintText("修改品类信息成功!");
        }else {
            log.error("修改品类信息失败!");
            throw new BaseBizException(ProcessCodeEnum.CATEGORY_UPDATE_EXCEPTION);
        }
    }

    @Override
    public CategoryDto getCategory(CategoryReq req) {
        CategoryPO categoryPO = checkCategory(req.getCategoryId());
        return BigGodConverter.convertCategoryPOToCategoryDTO(categoryPO);
    }

    @Override
    public List<CategoryDto> getCategoryList(CategoryReq req) {
        //默认获取前100条
        String keyword = StringUtils.parseKeyword(req.getKeyword());
        List<CategoryPO> categoryPOS = categoryMapper.getByName(keyword, 0, 100);
        if(categoryPOS.size() > 0){
            return BigGodConverter.convertCategoryPOToCategoryDTO(categoryPOS);
        }else {
            return Collections.emptyList();
        }
    }

    @Override
    public CategoryResp removeCategory(CategoryReq req) {
        CategoryPO categoryPO =  checkCategory(req.getCategoryId());
        List<Long> ids = Collections.singletonList(categoryPO.getCategoryId());
        boolean flag = categoryMapper.remove(ids) > 0;
        if(flag){
            return new CategoryResp().setHintText("删除品类信息成功!");
        }else {
            log.error("删除品类信息失败!");
            throw new BaseBizException(ProcessCodeEnum.CATEGORY_REMOVE_EXCEPTION);
        }
    }

    private CategoryPO checkCategory(String categoryId){
        if(StringUtils.isEmpty(categoryId)){
            log.error("品类ID不能为空：categoryId - {}", categoryId);
            throw new BaseBizException(ProcessCodeEnum.CATEGORY_ID_ISNULL);
        }
        CategoryPO categoryPO = categoryMapper.get(Long.valueOf(categoryId));
        if(categoryPO == null){
            log.error("当前品类不存在");
            throw new BaseBizException(ProcessCodeEnum.CATEGORY_NOT_REPETITION);
        }
        return categoryPO;
    }
}
